package cuongnh.project.japanesediary.vocabulary.ui.view.action.menucontribution.category.vocabularies;

import java.util.Set;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;

import cuongnh.project.japanesediary.vocabulary.common.CommonServices;
import cuongnh.project.japanesediary.vocabulary.model.Category;
import cuongnh.project.japanesediary.vocabulary.model.Vocabulary;
import cuongnh.project.japanesediary.vocabulary.ui.view.CategoryVocabulariesView;

public class RemoveCategoryVocabularyAction implements IViewActionDelegate {

	public static final String ID = "cuongnh.project.japanesediary.vocabulary.action.RemoveCategoryVocabulary";

	protected IViewPart m_view;

	private CommonServices commonServices;

	private Category category;

	private Set<Vocabulary> selectedVocabularies;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
	 */
	@Override
	public void init(IViewPart view) {
		m_view = view;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
	 */
	@Override
	public void run(IAction action) {
		// 履歴ビュー更新
		CategoryVocabulariesView view = (CategoryVocabulariesView) m_view
				.getAdapter(CategoryVocabulariesView.class);
		commonServices = view.getCommonServices();
		category = view.getSelectedCategory();
		selectedVocabularies = view.getSelectedVocabularies();
		if (!selectedVocabularies.isEmpty()) {
			boolean confirm = MessageDialog
					.openConfirm(view.getSite().getShell(),
							"Deleting confirmation!",
							"Are you sure you want to delete selected Vocabularies from current Category?");
			if (confirm) {
				// remove selected vocabulary to current category
				Category currentCategory = commonServices.getCategoryDao()
						.getCategoryWithVocabularies(category.getCategoryId());
				currentCategory.getVocabularies().removeAll(selectedVocabularies);
				commonServices.getCategoryDao().update(currentCategory);
				view.update();
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action
	 * .IAction, org.eclipse.jface.viewers.ISelection)
	 */
	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		
	}

}
